﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ElectronicRecords.Models
{
    public class ClientsRepository
    {
        private ElectronicRecordsDataContext _datacontext;

        public ClientsRepository(ElectronicRecordsDataContext d)
        {
            _datacontext = d;
        }

        public IEnumerable<Clients> GetClients()
        {
            return _datacontext.Clients.OrderBy(c => c.Firstname);
        }

        public Clients GetClient(int clientId)
        {
            return _datacontext.Clients.SingleOrDefault(c =>c.ID_Client==clientId);

        }

        public Clients CreateClient(string firstName, string lastName, string phone, string email)
        {
            Clients c = new Clients {Firstname=firstName, Lastname=lastName, Phone=phone, Email=email};
            _datacontext.Clients.InsertOnSubmit(c);
            _datacontext.SubmitChanges();
            return c;
        }

        public void UpdateClient(Clients c)
        {
            Clients dbClient = GetClient(c.ID_Client);
            dbClient.Firstname = c.Firstname;
            dbClient.Lastname = c.Lastname;
            dbClient.Phone = c.Phone;
            //dbClient.Requests = c.Requests;
            dbClient.aspnet_Users = c.aspnet_Users;
           // dbClient.CreatedOn = c.CreatedOn;
            dbClient.CreatedBy = c.CreatedBy;
            dbClient.Email = c.Email;
            _datacontext.SubmitChanges();
        }

        public void DeleteClient(int clientId)
        {
            Clients c=GetClient(clientId);
            _datacontext.Clients.DeleteOnSubmit(c);
            _datacontext.SubmitChanges();
        }

        public IEnumerable<Clients> FindClients(string firstname)
        {
            return GetClients().Where(c =>( c.Firstname.IndexOf(firstname)>-1)||(c.Lastname.IndexOf(firstname)>-1) ).AsEnumerable<Clients>();
        }

    }
}